# -*- mode: ruby -*-
# vi: set ft=ruby :

BOX_URL =  'https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box'.freeze

SWIFT_PATH = 'https://swift.org/builds/swift-3.1-release/ubuntu1404/swift-3.1-RELEASE'.freeze
SWIFT_DIRECTORY = 'swift-3.1-RELEASE-ubuntu14.04'.freeze
SWIFT_FILE = "#{SWIFT_DIRECTORY}.tar.gz".freeze
SWIFT_HOME = "/home/vagrant/#{SWIFT_DIRECTORY}".freeze

KITURA_URL = 'https://github.com/Kitura/Kitura.git'.freeze
KITURA_BRANCH = 'master'.freeze

Vagrant.configure(2) do |config|
  config.vm.box = BOX_URL
  config.vm.network 'forwarded_port', guest: 8080, host: 8080

  # Prevents "stdin: is not a tty" on Ubuntu (https://github.com/mitchellh/vagrant/issues/1673)
  config.vm.provision 'fix-no-tty', type: 'shell' do |s|
    s.privileged = false
    s.inline = "sudo sed -i '/tty/!s/mesg n/tty -s \\&\\& mesg n/' /root/.profile"
  end

  config.vm.provision 'shell', privileged: false, inline: <<-SHELL

### Install packages
# 1. Install compiler, autotools
    sudo apt-get --assume-yes install clang libicu-dev uuid-dev git
# 2. Install dtrace (to generate provider.h)
    sudo apt-get --assume-yes install systemtap-sdt-dev
# 3. Kitura packages
    sudo apt-get --assume-yes install libcurl4-openssl-dev


### Download Swift binary if not found, install it, and add it to the path
    if [ ! -f "#{SWIFT_FILE}" ]; then
        curl -O "#{SWIFT_PATH}/#{SWIFT_FILE}"
    fi
    tar zxf #{SWIFT_FILE}

if [ $(grep -c "#{SWIFT_FILE}" .profile) -eq 0 ]; then
        echo "export PATH=#{SWIFT_HOME}/usr/bin:\"${PATH}\"" >> .profile
        source .profile
    fi

### Download and install Kitura
    git clone #{KITURA_URL} -b #{KITURA_BRANCH}
    cd Kitura
    swift build
    cd ..

### Export LD_LIBRARY_PATH
    if [ $(grep -c "LD_LIBRARY_PATH=/usr/local/lib" .profile) -eq 0 ]; then
        echo "export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH" >> .profile
        source .profile
    fi

  SHELL
end
